package com.atguigu.java.ai.langchain4j.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.PageReadListener;
import com.atguigu.java.ai.langchain4j.entity.SystemData;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ExcelReader {
    public static List<SystemData> readExcel(String filePath) {
        List<SystemData> dataList = new ArrayList<>();

        if (!new File(filePath).exists()) {
            System.err.println("❌ 文件不存在: " + filePath);
            return Collections.emptyList();
        }

        EasyExcel.read(filePath, SystemData.class, new AnalysisEventListener<SystemData>() {
            @Override
            public void invoke(SystemData data, AnalysisContext context) {
                dataList.add(data);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                System.out.println("✅ 成功读取数据: " + dataList.size() + "条");
                System.out.println("数据列表: " + dataList);
            }
        }).sheet().doRead();

        return dataList.size() > 3 ?
                dataList.subList(3, dataList.size()) : dataList;
    }


}
